import json
import types
import json
import os
import codecs

weight = {'3':1,
        '4':2,
        '5':3,
        '6':4,
        '7':5,
        '8':6,
        '9':7,
        'T':8,
        'J':9,
        'Q':10,
        'K':11,
        'A':12,
        '2':13,
        'R':14,
        'B':15
    }
def space_str(layer):
    lua_str = ""
    for i in range(0,layer):
        lua_str += '\t'
    return lua_str

def dic_to_lua_str(data,layer=0):
    d_type = type(data)
    if d_type is str:
        return "'" + data + "'"
    elif d_type is bool:
        if data:
            return 'true'
        else:
            return 'false'
    elif d_type is int or d_type is float:
        return str(data)
    elif d_type is list:
        lua_str = "{\n"
        lua_str += space_str(layer+1)
        for i in range(0,len(data)):
            lua_str += dic_to_lua_str(data[i],layer+1)
            if i < len(data)-1:
                lua_str += ','
        lua_str += '\n'
        lua_str += space_str(layer)
        lua_str +=  '}'
        return lua_str
    elif d_type is dict:
        lua_str = ''
        lua_str += "\n"
        lua_str += space_str(layer)
        lua_str += "{\n"
        data_len = len(data)
        data_count = 0
        for k,v in data.items():
            data_count += 1
            lua_str += space_str(layer+1)
            if type(k) is int:
                lua_str += '[' + str(k) + ']'
            elif type(k) is str:
                lua_str += '["' + str(k) + '"]'
            else:
                lua_str += k 
            lua_str += ' = '
            try:
                lua_str += dic_to_lua_str(v,layer +1)
                if data_count < data_len:
                    lua_str += ',\n'
 
            except Exception:
                print ('error in ',k,v)
                raise
        lua_str += '\n'
        lua_str += space_str(layer)
        lua_str += '}'
        return lua_str
    else:
        print (d_type , 'is error')
        return None

def str_to_lua_table(jsonStr):
    data_dic = None
    try:
        data_dic = json.loads(jsonStr)
    except Exception:
        data_dic =[]
    else:
        pass
    finally:
        pass
    bytes = ''
    index = 0
    for k in data_dic:
        data_dic[k][0].append(index)
        index += 1

        if data_dic[k][0][0].startswith("trio_solo") or data_dic[k][0][0].startswith("trio_solo_chain") or data_dic[k][0][0].startswith("trio_pair") or data_dic[k][0][0].startswith("trio_pair_chain") or data_dic[k][0][0].startswith("four_two_solo") or data_dic[k][0][0].startswith("four_two_pair"):
            a = {}
            max = 0
            for i in range(0, len(k)):
                char = k[i]
                if char not in a.keys():
                    a[char] = []
                a[char].append(char)
                if len(a[char]) > max:
                    max = len(a[char])
            main = []
            extra = []
            for char, t in a.items():
                if len(t) == max:
                    for c in t:
                        main.append(c)
                else:
                    for c in t:
                        extra.append(c)
            main.sort(key=lambda c: weight[c])
            extra.sort(key=lambda c: weight[c])
            main.extend(extra)
            data_dic[k][0].append(''.join(main))

    for it in dic_to_lua_str(data_dic):
        bytes += it
    return bytes

def file_to_lua_file(jsonFile,luaFile):
    with codecs.open(luaFile,"w","utf-8") as luafile:
        with codecs.open(jsonFile,"r","utf-8") as jsonfile:
            luafile.write(str_to_lua_table(jsonfile.read()))

file_to_lua_file("F:/threecard-client/Games/club.threecard.doudizhu/card_type.json", "F:/threecard-client/Games/club.threecard.doudizhu/card_type.lua")